package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.TemplateConfig;
import com.burgeon.backend.SHOPBACKEND.service.BackendTemplateService;
import com.burgeon.backend.SHOPBACKEND.service.ex.UpDataException;
import com.burgeon.backend.Util.RunnerDate;
import com.burgeon.backend.entity.Coupon;
import com.burgeon.backend.mappers.CouponMapper;
import com.burgeon.backend.mappers.TemplateMapper;
import com.burgeon.backend.service.MessageTemplateService;
import com.burgeon.backend.service.QueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BackendTemplateServiceImpl implements BackendTemplateService {
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private QueueService queueService;
    @Autowired
    private MessageTemplateService messageTemplateService;
    @Autowired
    private TemplateMapper templateMapper;

    @Override
    public void GetExpiredCouponTemplate(TemplateConfig templateConfig) {
        Calendar calendar1 = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        calendar1.add(Calendar.DATE, +3);
        String expiredTime = dateFormat.format(calendar1.getTime());//过期时间
        System.err.println(expiredTime);
        List<Coupon> coupons = couponMapper.GetCouponByExpiredTime(expiredTime,templateConfig.getAccount());
        System.err.println(coupons);
        for(Coupon coupon:coupons){
            System.err.println(coupon);
            queueService.putExpiredCouponTemplate(templateConfig.getTemplateId(),templateConfig.getAccount(),
                    coupon.getCode(),coupon.getMoney(),coupon.getDateend(),coupon.getOpenid(),coupon.getId());
        }

    }

    @Override
    public void SendExpiredCouponTemplate(String templateId, String token, String code, String money, String expiredTime, String openid, String id) {
        System.err.println("dddddd");
        boolean state = messageTemplateService.SendExpiredCouponTemplate(templateId, token, code, money, expiredTime, openid);
        if(state){
            couponMapper.updateIsSend(id);
        }
    }

    @Override
    public Integer UpdateTemplate8Config(String token, String sendTime, String intervalTime) {
        Integer row = templateMapper.UpdateTemplate8Config(sendTime,intervalTime);
        if(row !=0){
            throw new UpDataException("修改失败");
        }
        RunnerDate aObj = new RunnerDate();
        Field bField = null;
        try {
            bField = aObj.getClass().getDeclaredField("date2");
            bField.setAccessible(true);
            bField.set(aObj, TemplateDate("qdld"));
            System.err.println(bField.get(aObj)+","+aObj.getDate2());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return row;
    }

    public Date TemplateDate(String token){
        TemplateConfig templateConfig = templateMapper.GetTemplate8(token);
        System.err.println(templateConfig);
        Calendar calendar7 = Calendar.getInstance();
        //获取发送时间
        calendar7.set(Calendar.HOUR_OF_DAY,Integer.parseInt(templateConfig.getSendTime()));
        Date date1=calendar7.getTime(); //第一次执行定时任务的时间
        if (date1.before(new Date())) {
            date1 = this.addDay(date1, 1);
        }
        System.out.println(date1);
        return date1;

    }

    // 增加或减少天数
    private Date addDay(Date date, int num) {
        Calendar startDT = Calendar.getInstance();
        startDT.setTime(date);
        startDT.add(Calendar.DAY_OF_MONTH, num);
        return startDT.getTime();
    }
}
